package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.target.TargetData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemRelationMapper
 * @Description: 系统关联表
 * @auther xukl
 * @data 2021/3/31  15:05
 * @Version V1.0
 */
public interface SystemRelationMapper {

    /**
     * 查询系统关联的目标与名称
     */
    @Select(value = {
            "SELECT a.target_id AS targetId,a.target_name AS targetName,a.target_type AS targetClassify,a.lon,a.lat,a.height FROM ",
            "(SELECT target_id,target_name,target_classify,lon,lat,height FROM target_base_data WHERE delete_sign = 1) AS a ",
            "JOIN ",
            "(SELECT target_id FROM system_relation WHERE s_id = #{sId}) AS b ",
            "ON ",
            "a.target_id = b.target_id"
    })
    List<Map<String, Object>> selectSystemRelationTargetIdAndName(@Param("sId") String sId);

    @Select(value = {
            "SELECT a.target_id,a.target_name,a.lon,a.lat,a.height,a.target_classify,a.type FROM ",
            "(SELECT target_id,target_name,target_classify,lon,lat,height FROM target_base_data WHERE delete_sign = 1) AS a ",
            "JOIN ",
            "(SELECT target_id FROM system_relation WHERE s_id = #{sId}) AS b ",
            "ON ",
            "a.target_id = b.target_id"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "targetName", column = "target_name"),
            @Result(property = "targetClassify", column = "target_classify")
    })
    List<TargetData> selectSystemRelationTarget(@Param("sId") String sId);

    @Select(value = {
            "SELECT a.target_id,a.target_name,a.lon,a.lat,a.height,a.target_classify FROM ",
            "(SELECT target_id,target_name,target_classify,lon,lat,height FROM target_base_data WHERE delete_sign = 1) AS a ",
            "JOIN ",
            "(SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_type = #{type}) AS b ",
            "ON ",
            "a.target_id = b.target_id"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "targetName", column = "target_name"),
            @Result(property = "targetClassify", column = "target_classify")
    })
    List<TargetData> selectSystemsRelationTarget(@Param("sysId") String sId, @Param("type") String type);

    @Select(value = "SELECT target_id FROM system_relation WHERE s_id = #{sId}")
    List<String> selectSystemRelationTargetId(@Param("sId") String sId);

    @Select(value = {
            "<script>",
            "SELECT target_id FROM system_relation WHERE s_id IN ",
            "<foreach collection='sIds' item='item' index='index' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    List<String> selectSystemsRelationTargetId(@Param("sIds") List<String> sIds);

}


